home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1833 / 1833.xpi / chrome / yoono.jar / content / yoono / bookmarks / bookmarksio.js < prev    next >
Text File  |  2009-12-16  |  6KB  |  217 lines

  1. /*
  2.  * bookmarkio.js
  3.  *
  4.  * @author    david marteau <marteau.david@free.fr>
  5.  * @copyright 2005-2006 Yoono
  6.  */
  7.  
  8. var ynBookmarksIODrivers = 
  9. {
  10.   // XBEL
  11.   xbel: {
  12.     toXML : function() {
  13.       return (new ynXBELIOService()).exportBookmarks();
  14.     },
  15.  
  16.     fromXML : function( xmlNode, baseURI ) {
  17.       new ynXBELIOService().importBookmarks(xmlNode,null);  
  18.     },
  19.     
  20.     get fileExt() { return "xbel"; }
  21.   },
  22.   
  23.   // OPML2
  24.   opml: {
  25.     toXML : function() {
  26.       var ownerName;
  27.       var ownerEmail;
  28.       var ownerId;
  29.       return (new ynOPMLIOService()).exportBookmarks(ownerName,ownerEmail,ownerId );
  30.     },
  31.  
  32.     fromXML : function( xmlNode, baseURI ) {
  33.       new ynOPMLIOService().importBookmarks(xmlNode,null,baseURI);  
  34.     },
  35.     
  36.     get fileExt() { return "opml"; }
  37.  
  38.   }
  39. };
  40.  
  41. var ynBookmarksIO = 
  42. {
  43.   _preference : "extensions.yoono.bookmark.exportdir",
  44.   _bundle     : null,
  45.  
  46.   _getFile : function( title, mode, defaultString ) 
  47.   {
  48.     const nsIFilePicker = Components.interfaces.nsIFilePicker;
  49.  
  50.     mode = (mode=="modeOpen"?
  51.             nsIFilePicker.modeOpen:
  52.             nsIFilePicker.modeSave);
  53.  
  54.     var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  55.                  .getService(Components.interfaces.nsIPrefBranch);
  56.                  
  57.     var fp = Components.classes["@mozilla.org/filepicker;1"]
  58.                  .createInstance(nsIFilePicker);
  59.                  
  60.     fp.init(window,title, mode);  
  61.  
  62.     if(mode == nsIFilePicker.modeOpen) 
  63.     {
  64.       fp.appendFilter(this.getString("xbelfiles") ,"*.xbel");
  65.       fp.appendFilter(this.getString("opmlfiles") ,"*.opml");
  66.       fp.appendFilters(fp.filterXML);
  67.     }
  68.  
  69.     fp.appendFilters(fp.filterAll);
  70.  
  71.     if(defaultString) 
  72.        fp.defaultString = defaultString;
  73.  
  74.     var dir = null;
  75.     try {
  76.       dir = prefs.getComplexValue(this._preference,Components.interfaces.nsILocalFile);
  77.     } catch (e) {}
  78.       
  79.     // fallback to home dir  
  80.     if(dir==null) {
  81.        dir = Components.classes["@mozilla.org/file/directory_service;1"].
  82.               getService(Components.interfaces.nsIProperties).get("Home",Components.interfaces.nsIFile);
  83.     }    
  84.  
  85.     if(dir && dir.exists())
  86.        fp.displayDirectory = dir;
  87.        
  88.     var ret = fp.show();
  89.     if( ret == nsIFilePicker.returnOK || 
  90.         ret == nsIFilePicker.returnReplace) 
  91.     {
  92.       var results = [];    
  93.       var nsILocalFile = Components.interfaces.nsILocalFile;
  94.  
  95.       var file = fp.file;
  96.       var dir  = file.parent.QueryInterface(nsILocalFile);
  97.  
  98.       prefs.setComplexValue(this._preference,Components.interfaces.nsILocalFile,dir);
  99.       return file;         
  100.     }
  101.     
  102.     return null;  
  103.   },  
  104.  
  105.   getString : function( aString )
  106.   {
  107.     if(this._bundle == null) {
  108.        this._bundle = document.getElementById("yoono-bmks-bundle");
  109.     }
  110.     return this._bundle.getString(aString);
  111.   },
  112.  
  113.   getExportType : function()
  114.   {
  115.     var args = {};
  116.     window.openDialog('chrome://yoono/content/bookmarks/exporttype.xul',
  117.                       '', 'chrome,modal',args); 
  118.  
  119.     return args.value;
  120.   },
  121.  
  122.   exportBookmarks : function()
  123.   {
  124.     const XML10_UTF8 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n\n";
  125.   
  126.     var exportType = this.getExportType();    
  127.     if(!exportType)
  128.         return;
  129.  
  130.     var driver = ynBookmarksIODrivers[exportType];
  131.   
  132.     var title = this.getString("bookmarks.export.title");    
  133.     var file  = this._getFile(title,"modeSave","bookmarks."+driver.fileExt);
  134.     
  135.     if(file) 
  136.     {
  137.        var xbel = driver.toXML();
  138.        var fos  = Components.classes["@mozilla.org/network/file-output-stream;1"]
  139.                    .createInstance(Components.interfaces.nsIFileOutputStream);
  140.  
  141.        var xmlContent = xbel.toXMLString();
  142.        fos.init(file, -1, -1, false);
  143.        fos.write(XML10_UTF8,XML10_UTF8.length);
  144.        var converter = CL["@mozilla.org/intl/converter-output-stream;1"].createInstance( CI.nsIConverterOutputStream); 
  145.        converter.init(fos, 'UTF-8', 1024, '-');
  146.        converter.writeString(xmlContent);
  147.        converter.flush();
  148.        fos.close();  
  149.     }
  150.   },
  151.  
  152.   importBookmarks : function()
  153.   {
  154.     var title = this.getString("bookmarks.import.title");
  155.     var file  = this._getFile(title,"modeOpen","");
  156.  
  157.     if(file)
  158.     {
  159.       //------------
  160.       // Read file 
  161.       //------------
  162.       const MODE_RDONLY  = 0x01;
  163.       const PERMS_FILE   = 0644;
  164.  
  165.       var fileStream = Components.classes["@mozilla.org/network/file-input-stream;1"]
  166.                                .createInstance(Components.interfaces.nsIFileInputStream);
  167.       
  168.       fileStream.init(file, MODE_RDONLY, PERMS_FILE, 0);
  169.  
  170.       var sis = Components.classes["@mozilla.org/scriptableinputstream;1"].
  171.                   createInstance(Components.interfaces.nsIScriptableInputStream);
  172.      
  173.       sis.init(fileStream); 
  174.       var parser = new DOMParser();
  175.       var xmldoc = parser.parseFromStream(fileStream, 'UTF-8', sis.available(),"text/xml");
  176.       fileStream.close();
  177.       sis.close();
  178.  
  179.       // Get the uri from file
  180.       var ioSrvc   = Components.classes["@mozilla.org/network/io-service;1"]
  181.                         .getService(Components.interfaces.nsIIOService);
  182.       var baseURI  = ioSrvc.newFileURI(file);
  183.       var root = xmldoc.documentElement;
  184.       if ((root.tagName == "parsererror") ||
  185.           (root.namespaceURI == "http://www.mozilla.org/newlayout/xml/parsererror.xml"))
  186.       {
  187.         alert(this.getString("xmlparsererror"));
  188.         return;
  189.       }
  190.      
  191.       var xmlnode = null;
  192.       var driver  = null;
  193.  
  194.       for( var type in ynBookmarksIODrivers) {
  195.          xmlnode = xmldoc.getElementsByTagName(type)[0];
  196.          if(xmlnode) {
  197.            driver = ynBookmarksIODrivers[type];
  198.            break;
  199.          }
  200.       }
  201.  
  202.       if(!xmlnode) {
  203.         alert(this.getString("notvaliddocument"));
  204.         return;
  205.       }
  206.  
  207.       var xmlstring = new XMLSerializer().serializeToString(xmlnode);
  208.       var xmlelement= new XML(xmlstring);
  209.  
  210.       driver.fromXML(xmlelement,baseURI);
  211.  
  212.       //alert(this.getString("bookmarksimported"));
  213.     }
  214.   }
  215.   
  216. }
  217.